package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.ShopGuiderContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.ShopGuiderBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.ShopperGuideActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amy on 2017/11/27.
 */

public class ShopGuiderPresenter implements ShopGuiderContract.Presenter {

    private ShopperGuideActivity mContext;
    private ShopGuiderContract.View mView;

    public ShopGuiderPresenter(ShopperGuideActivity activity, ShopperGuideActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getShopGuiderList(int pageIndex, int pageSize) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");
        Map<String, String> map = new HashMap<>();
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        MineApiDataSource.getInstance()
                .getShopGuider(InfoProvider.getUserToken(mContext).getUser_token(),
                        "saleAssistant.consumer.saleAssistantForConsumerListPageFind",
                        new Gson().toJson(maps), new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<ShopGuiderBean>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<ShopGuiderBean> response) {
                        List<ShopGuiderBean.Convient> shopGuiderList = response.data.getList();
                        if (shopGuiderList != null && shopGuiderList.size() > 0) {
                            // 有数据
                            mView.getShopGuiderListSucc(shopGuiderList);

                        } else {
                            // 没有数据，显示为空
                            // 当第一次拉取数据时
                            if (pageIndex == 1)
                                mView.showEmpty();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    /**
     * 设置解绑定事件
     */
    @Override
    public void clickBindEvent(ShopGuiderBean.Convient convient) {
        // 取消绑定
        Map<String, String> map = new HashMap<>();
        map.put("stores_id", convient.getStores_id());
        map.put("stores_name", convient.getStores_name());
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());
        map.put("consumer_mobile", InfoProvider.getUserToken(mContext).getMobile());

        MineApiDataSource.getInstance()
                .deleteShopGuider(InfoProvider.getUserToken(mContext).getUser_token(),
                        "saleAssistant.consumer.saleAssistantForConsumerCancel",
                        new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.deleteGuiderSucc(convient);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

}
